package com.sx.micowell.module.course

import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.BaseActivity
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.LogUtils
import com.miloyu.modelnet.response.getResDrawable
import com.miloyu.modelnet.response.getResId
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragRecommendCourseBinding
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RoutePath.Companion.RECOMMEND_COURSE
import com.sx.micowell.framework.model.RecommendCourseModel
import com.sx.micowell.module.MainActivity
import com.sx.widget.view.flyco.tablayout.SlidingTabLayout
import com.sx.widget.view.flyco.tablayout.listener.OnTabSelectListener
import kotlinx.android.synthetic.main.frag_recommend_course.*
import java.util.*
import kotlin.streams.toList

/**
 * @Description 推荐课程
 * @Author 梁浩文
 * @Date 2021/8/24 10:30
 */
@Route(path = RECOMMEND_COURSE)
class RecommendCourseFragment : MyFragment<FragRecommendCourseBinding, MainActivity>() {

    lateinit var viewModel: RecommendCourseModel

    override fun initViewModel() {
        viewModel=getFragmentScopeViewModel(RecommendCourseModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig =
        DataBindingConfig(R.layout.frag_recommend_course,BR.viewModel,viewModel)
            .addBindingParam(BR.manager,childFragmentManager)

    override fun initView() {

    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.courseTypeList.observe(this,{
            var _on="_on"
            var _off="_off"
            var drawable="drawable"
            var arrayList = it.stream().map {
                CourseTabEntity(
                    context?.getResId(drawable, it.dictValue.lowercase(Locale.getDefault()) + _on) as Int,
                    context?.getResId(drawable, it.dictValue.toLowerCase() + _off) as Int,
                    it.dictLabel
                )
            }.toList()
            course_tab.setTabData(arrayList)
            viewModel?.initPage()
        })
    }

    override fun initData() {
        viewModel?.reqCourseType()
        course_tab.setOnTabSelectListener(object :OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                course_vp.currentItem=position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        course_vp.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                course_tab.currentTab=position;
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
    }
}